<!--
  * 采购订单表
  *
  * @Author:    zzw
  * @Date:      2024-10-08 17:37:12
  * @Copyright  jz
-->
<template>
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">
        <a-row class="smart-query-form-row">
            <a-form-item label="采购订单编码" class="smart-query-form-item">
                <a-input style="width: 150px" v-model:value="queryForm.orderCode" placeholder="" />
            </a-form-item>
            <a-form-item label="采购入库单编码" class="smart-query-form-item">
                <a-input style="width: 150px" v-model:value="queryForm.inboundCode" placeholder="" />
            </a-form-item>
            <a-form-item label="入库时间" class="smart-query-form-item">
                <a-range-picker v-model:value="queryForm.orderTime" :presets="defaultTimeRanges" style="width: 150px"
                    @change="onChangeOrderTime" />
            </a-form-item>
            <a-form-item label="入库状态" class="smart-query-form-item">
                <a-select v-model:value="queryForm.inboundStatus" style="width: 150px"
                    :options="[{ value: 0, label: '待入库' }, { value: 1, label: '已入库' }]"></a-select>
            </a-form-item>
            <a-form-item class="smart-query-form-item">
                <a-button type="primary" @click="queryData">
                    <template #icon>
                        <SearchOutlined />
                    </template>
                    查询
                </a-button>
                <a-button @click="resetQuery" class="smart-margin-left10">
                    <template #icon>
                        <ReloadOutlined />
                    </template>
                    重置
                </a-button>
            </a-form-item>
        </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">
        <!---------- 表格操作行 begin ----------->
        <a-row class="smart-table-btn-block">
            <div class="smart-table-operate-block">
                <a-button @click="showForm" type="primary" size="small">
                    <template #icon>
                        <PlusOutlined />
                    </template>
                    新建
                </a-button>
                <!-- <a-button @click="confirmBatchDelete" type="danger" size="small"
                    :disabled="selectedRowKeyList.length == 0">
                    <template #icon>
                        <DeleteOutlined />
                    </template>
                    批量删除
                </a-button> -->
            </div>
            <div class="smart-table-setting-block">
                <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
            </div>
        </a-row>
        <!---------- 表格操作行 end ----------->

        <!---------- 表格 begin ----------->
        <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading"
            :pagination="false" :scroll="{ x: 1100 }"
            :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
            <template #bodyCell="{ text, record, column }">
                <!-- orderStatus -->
                <template v-if="column.dataIndex === 'inboundStatus'">
                    {{ ['待入库', '已入库'][record.inboundStatus] }}
                </template>
                <template v-if="column.dataIndex === 'action'">
                    <div class="smart-table-operate">
                        <a-button @click="showForm(record)" type="link" v-if="record.inboundStatus != 1">编辑</a-button>
                        <a-button @click="showForm2(record)" type="link">详情</a-button>
                        <a-button @click="approve(record)" type="link"
                            v-if="record.approverId == employeeId && record.inboundStatus == 0">审批</a-button>
                        <a-button @click="onDelete(record)" danger type="link"
                            v-if="record.inboundStatus != 1">删除</a-button>

                    </div>
                </template>
            </template>
        </a-table>
        <!---------- 表格 end ----------->

        <div class="smart-query-table-page">
            <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
                :defaultPageSize="queryForm.pageSize" v-model:current="queryForm.pageNum"
                v-model:pageSize="queryForm.pageSize" :total="total" @change="queryData" @showSizeChange="queryData"
                :show-total="(total) => `共${total}条`" />
        </div>
        <PurchaseOrderForm ref="formRef" @reloadList="queryData" />

    </a-card>

    <a-modal v-model:visible="visible" title="审批" @ok="handleOk">
        <a-form>
            <a-form-item has-feedback label="审批结论" name="pass">
                <a-radio-group v-model:value="approveObj.type">
                    <a-radio :value="1">批准</a-radio>
                    <a-radio :value="2">不批准</a-radio>
                </a-radio-group>
            </a-form-item>
            <!-- reason -->
            <!-- <a-form-item v-if="approveObj.type === 2" has-feedback label="审批意见" name="pass">
                <a-textarea v-model:value="approveObj.reason" placeholder="textarea with clear icon" allow-clear />
            </a-form-item> -->
        </a-form>
    </a-modal>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { purchaseInboundApi } from '/@/api/business/purchaseInventory/index.js';
import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { smartSentry } from '/@/lib/smart-sentry';
import TableOperator from '/@/components/support/table-operator/index.vue';
import { defaultTimeRanges } from '/@/lib/default-time-ranges';
import PurchaseOrderForm from './components/form.vue';
import { useUserStore } from '/@/store/modules/system/user.js'
import { storeToRefs } from 'pinia'
const store = useUserStore()


const employeeId = storeToRefs(store).employeeId.value
// ---------------------------- 表格列 ----------------------------
const columns = ref([
    {
        title: '入库单编码',
        dataIndex: 'inboundCode',
        width: 120,
        ellipsis: true,
    },
    {
        title: '备注',
        dataIndex: 'remark',
        width: 120,
        ellipsis: true,
    },
    {
        title: '附件',
        dataIndex: 'fileUrl',
        width: 120,
        ellipsis: true,
    },
    {
        title: '入库状态',
        dataIndex: 'inboundStatus',
        width: 120,
        ellipsis: true,
    },
    {
        title: '审批人姓名',
        dataIndex: 'approverName',
        width: 120,
        ellipsis: true,
    },
    {
        title: '操作',
        dataIndex: 'action',
        fixed: 'right',
        width: 135,
    }
]);

// ---------------------------- 查询数据表单和方法 ----------------------------

const queryFormState = {
    orderCode: undefined, //采购订单编号
    inboundCode: undefined, //采购入库单编号
    orderTime: [], //入库时间
    inboundTimeBegin: undefined, //入库时间 开始
    inboundTimeEnd: undefined, //入库时间 结束
    inboundStatus: undefined, //入库状态
    pageNum: 1,
    pageSize: 10,
};
// 查询表单form
const queryForm = reactive({ ...queryFormState });
// 表格加载loading
const tableLoading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数
const total = ref(0);

// 重置查询条件
function resetQuery() {
    let pageSize = queryForm.pageSize;
    Object.assign(queryForm, queryFormState);
    queryForm.pageSize = pageSize;
    queryData();
}

// 查询数据
async function queryData() {
    tableLoading.value = true;
    try {
        let queryResult = await purchaseInboundApi.queryPage(queryForm);
        tableData.value = queryResult.data.list;
        total.value = queryResult.data.total;
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
        tableLoading.value = false;
    }
}
// 时间选择
function onChangeOrderTime(dates, dateStrings) {
    queryForm.inboundTimeBegin = dateStrings[0];
    queryForm.inboundTimeEnd = dateStrings[1];
}


onMounted(queryData);

// ---------------------------- 添加/修改 ----------------------------
const formRef = ref();

function showForm(data) {
    formRef.value.show(data);
}

function showForm2(data) {
    data.type = 3
    formRef.value.show(data);
}

// ---------------------------- 单个删除 ----------------------------
//确认删除
function onDelete(data) {
    Modal.confirm({
        title: '提示',
        content: '确定要删除选吗?',
        okText: '删除',
        okType: 'danger',
        onOk() {
            requestDelete(data);
        },
        cancelText: '取消',
        onCancel() { },
    });
}

//请求删除
async function requestDelete(data) {
    SmartLoading.show();
    try {
        let deleteForm = {
            goodsIdList: selectedRowKeyList.value,
        };
        await purchaseInboundApi.delete(data.id);
        message.success('删除成功');
        queryData();
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
        SmartLoading.hide();
    }
}

// ---------------------------- 批量删除 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys) {
    selectedRowKeyList.value = selectedRowKeys;
}

// 批量删除
function confirmBatchDelete() {
    Modal.confirm({
        title: '提示',
        content: '确定要批量删除这些数据吗?',
        okText: '删除',
        okType: 'danger',
        onOk() {
            requestBatchDelete();
        },
        cancelText: '取消',
        onCancel() { },
    });
}

//请求批量删除
async function requestBatchDelete() {
    try {
        SmartLoading.show();
        await purchaseInboundApi.batchDelete(selectedRowKeyList.value);
        message.success('删除成功');
        queryData();
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
        SmartLoading.hide();
    }
}

//审批
const visible = ref(false);
const approveObj = reactive({
    id: '',
    type: '',
    reason: ''
})
function approve(record) {
    visible.value = true;
    approveObj.id = record.id
}
function handleOk() {
    purchaseInboundApi.approve(approveObj.id).then(res => {
        message.success('成功');
        visible.value = false;
    });
}

onMounted(() => {

})
</script>
